package org.hgvc.webapp.ets.jsf.beans.admin;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;

import com.j6.framework.util.J;


public class AdminViewMemberInfoBean extends AbstractBean {

	public static final String BEAN_NAME="adminViewMemberInfoBean";
	public static final String AB_ACTION_INVOKE="assignMemberAgreementAction";
	public static final String NAV_COMP_ASSGIN_AGREEMENT="adminCompMemberAgreeAssign";
	public static final String NAV_IND_ASSGIN_AGREEMENT="adminIndMemberAgreeAssign";
	
	private Member member;
	private Agreement agreement;
	private MemberInd memberInd;
	private MemberComp memberComp;
	private MemberService service;
	private String actionNo;
	
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public Agreement getAgreement() {
		return agreement;
	}
	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}
	public MemberInd getMemberInd() {
		return memberInd;
	}
	public void setMemberInd(MemberInd memberInd) {
		this.memberInd = memberInd;
	}
	public MemberComp getMemberComp() {
		return memberComp;
	}
	public void setMemberComp(MemberComp memberComp) {
		this.memberComp = memberComp;
	}
	public void setService(MemberService service) {
		this.service = service;
	}
	boolean newAgreementExistingMember=false;
	/*-----------------------------------------------------------*/
	public String assignMemberAgreementAction(String actionNo,String... params)throws Exception{
		
		System.out.println("assignMemberAgreementAction---");
		
		String outcome=null;
		this.actionNo = actionNo;
		String memberId = params[1];
		String agreementId = params[2]; 
		newAgreementExistingMember = Boolean.parseBoolean( params[3].toLowerCase());
		try{
			member = service.getMemberById(memberId);
			agreement =  (Agreement)commonDao.getObject(Agreement.class, agreementId);
			if (agreement.getCoolingOffPeriodInt()==0)
				agreement.setCoolingOffPeriodInt(10);
			if(member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_COMP)){
				memberComp = service.getMemberCompByMemberId(member.getMemberId());
				outcome= NAV_COMP_ASSGIN_AGREEMENT.concat("_"+getRoleName());
			}else{
				memberInd = service.getMemberIndByMemberId(member.getMemberId());
				memberComp = service.getMemberCompByMemberId(member.getMemberId());
				outcome= NAV_IND_ASSGIN_AGREEMENT.concat("_"+getRoleName());
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		System.out.println("assignMemberAgreementAction--outcome-"+outcome);
		
		return outcome;
	}
	
	
	public String navBackAction(){
		return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
	}
	
	public String assignAction() throws Exception{
		try{
			service.generateMemberAgreement(member, agreement, getThisUser(),actionNo,newAgreementExistingMember);
			return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return null;
	}
}
